<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
		parent::__construct();
	}
	
	public function search(){
		//$this->load->helper('url');
		//$this->load->view('search');
	}
	
	public function index2()
	{
		//$a = $this->m_model->getRows('select * from blogs limit 19');
		//print_r($a);
		//echo 'sd';exit;
		   
    //$data['blogs'] = $a;
		//$this->load->view('search', $data);
	}
	
    public function index(){
		//echo 'sd';exit;
        //header('content-type: text/html;charset=utf-8');
		//$words = $_GET['s'];
        $words = $this->input->get('s');
        if($words===NULL) $words = '读书的乐趣';
        $this->load->library('sphinxclient', NULL, 'sphinx');
        $index = "test1";
        $opts = array
		(
                "before_match"      => '<span style="color:red;">',
                "after_match"       => "</span>",
                "chunk_separator"   => " ... ",
                "limit"             => 60,
                "around"            => 3,
        );
        $this->sphinx->SetServer('127.0.0.1',9312);
        $this->sphinx->SetConnectTimeout(3);
        $this->sphinx->SetArrayResult(TRUE);
        $this->sphinx->SetMatchMode(SPH_MATCH_ANY);
        $this->sphinx->SetLimits(0,20);
		//搜索所有
		//$res = $this->sphinx->Query($words, '*');

		//搜索一个字段
		$this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
		$field = 'title';
		$res=$this->sphinx->Query("@".$field.' '.$words,"*"); 
		
		//$res=cl->Query( '@title (测试) @content ('网络')' , "*");

        if($res===FALSE){
                var_dump($this->sphinx->GetLastError());
                exit;
        }
        /*
		echo "关键词 <b>{$words}</b> ，找到约 <b>{$res['total_found']}</b> 结果，用时 <b>{$res['time']}</b>s";
        echo '<br/><hr/><br/>';
        if(array_key_exists('words', $res) && is_array($res['words'])){
                foreach($res['words'] as $k => $v){
                echo $k . ' : ' . $v['docs'] . ' - ' . $v['hits'] . '<br/>';
			}
        }
        echo '<br/><hr/><br/>';*/
		$this->sphinx->Close();
        $idarr = array();
        if(array_key_exists('matches', $res) && is_array($res['matches'])){
                foreach($res['matches'] as $v){
                        $idarr[] = $v['id'];
                }
        }
		//echo '<pre>';
		//print_r($idarr);
		if(count($idarr)>0){
			$ids = '';
			$ids =  join(',',array_values($idarr));
		$a = $this->m_model->getRows('select * from blogs where id in (' . $ids . ')');
		//print_r($a);
		//echo 'sd';exit;
		$data['blogs'] = $a;
		$this->load->view('search', $data);
		}
		/*
        if(count($idarr)>0){
                $this->db->from('shop_goods_info');
                $this->db->select('pname,cretime');
                $this->db->where_in('id', $idarr);
                $result = $this->db->get()->result_array();
                echo '<ul>';
                $name_arr = array();
                foreach($result as $k=>$v){
                        $name_arr[$k] = $v['pname'];
                }
                $name_arr = $this->sphinx->BuildExcerpts($name_arr, $index, $words, $opts);
                foreach($result as $k=>$v){
                        echo '<li>' . $name_arr[$k] . '(' . date('Y-m-d H:i:s', $v['cretime']) . ')</li>';
                }
                echo '</ul>';
        }*/
        
	}
}
